﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using Cisoft.IntegrationSystem.BLL;
using Cisoft.IntegrationSystem.Model;

public partial class Business_Cisoft3G_UserManage3G_AddTest : WebUI.Page.BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        divMessageforloginnameservice.InnerHtml = "&nbsp;";
        if (!Page.IsPostBack)
        {
            GetRole();
            ChuShiHuaData();
        }
    }

    #region 初始绑定管理员角色 public void GetRole()
    /// <summary>
    /// 初始绑定管理员角色
    /// </summary>
    /// <returns></returns>
    /// 2009.9.3 ryan
    public void GetRole()
    {
        Cisoft.IntegrationSystem.BLL.UserRoleBO rolebo = new Cisoft.IntegrationSystem.BLL.UserRoleBO();
        DataSet dst = rolebo.GetList(" [UR_RoleName] not like '%会员%' ");
        if (dst != null)
        {
            ddlUserGroupParent.DataSource = dst;
            ddlUserGroupParent.DataTextField = "UR_RoleName";
            ddlUserGroupParent.DataValueField = "UR_RoleID";
            ddlUserGroupParent.DataBind();
        }
        else
        {
            ddlUserGroupParent.Enabled = false;
        }
    }
    #endregion

    #region 添加事件 protected void ibtnadd_Click(object sender, ImageClickEventArgs e)
    /// <summary>
    /// 添加事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnadd_Click(object sender, EventArgs e)
    {
        //lblPhone.Text = "";

        divMessageforloginnameservice.InnerHtml = "&nbsp;";
        divMessageforloginnameservice.InnerHtml = "&nbsp;";
        // Session["MemberCheckCode"] = "13";    //仅赋初始值便于页面加载时候脚本能获取对象而不出错
        if (txtMemberNameforregiester.Text != "")
        {
            Cisoft.IntegrationSystem.BLL.MemberBO bllMember = new Cisoft.IntegrationSystem.BLL.MemberBO();
            Cisoft.IntegrationSystem.BLL.UserBO bllUser = new Cisoft.IntegrationSystem.BLL.UserBO();
            if (!bllMember.CheckMemberLoginName(txtMemberNameforregiester.Text.Trim().ToString()) && !bllUser.CheckUserLoginName(txtMemberNameforregiester.Text.Trim().ToString()))
            {

                Cisoft.IntegrationSystem.BLL.UserBO blluserinsert = new Cisoft.IntegrationSystem.BLL.UserBO();
                Cisoft.IntegrationSystem.Model.UserMO modeluserinsert = new Cisoft.IntegrationSystem.Model.UserMO();
                Cisoft.IntegrationSystem.Common.MD5 md5 = new Cisoft.IntegrationSystem.Common.MD5();
                string strloginname = "";
                //用户名
                strloginname = txtMemberNameforregiester.Text.Trim().ToString();
                string strpwd = "888888";

                strpwd = ConfigMg.UIConfig.cisforgetpwd.ToString();
                //密码
                strpwd = pwdBox.Text.Trim().ToString();
                //md5加密处理
                strpwd = md5.HashString(strpwd);
                string strrealname = "";
                //真实姓名
                strrealname = txtRealName.Text.Trim().ToString();
                modeluserinsert.UserName = strloginname;
                modeluserinsert.UserPassword = strpwd;
                modeluserinsert.UserRealName = strrealname;
                modeluserinsert.UserApplyDate = DateTime.Now;
                //角色设置
                modeluserinsert.Userrole = int.Parse(ddlUserGroupParent.SelectedValue.ToString());
                modeluserinsert.UserGroupID = int.Parse(ddl_Unit.SelectedValue);
                //}
                //modeluserinsert.UserGroupID = 1;                             //暂不分组 为1
                int nresultforend = -1;

                try
                {
                    nresultforend = blluserinsert.AddRecord(modeluserinsert);

                    //管理员信息
                    Cisoft.IntegrationSystem.BLL.UserInfoBO blluserInfoinsert = new Cisoft.IntegrationSystem.BLL.UserInfoBO();
                    Cisoft.IntegrationSystem.Model.UserInfoMO modeluserInfoinsert = new Cisoft.IntegrationSystem.Model.UserInfoMO();
                    modeluserInfoinsert.UI_Phone = txtPhone.Text.Trim().Replace("'", "").ToString();
                    modeluserInfoinsert.UI_JoblName = ddlUserGroupParent.Text;//txtJoblName.Text.Trim().Replace("'", "").ToString();
                    modeluserInfoinsert.UI_UserID_FK = blluserinsert.GetMaxID() - 1;
                    blluserInfoinsert.Add(modeluserInfoinsert);
                    //添加至联系人
                    AddToContacts(modeluserinsert.UserRealName, modeluserInfoinsert.UI_Phone);
                }
                catch (Exception ex)
                {
                    string strerror = "添加失败！请联系管理员，以下为提示信息：" + ex.Message.ToString();
                    //PageError(strerror, "UserManage.aspx", "返回管理", "");
                    btnBack_ts.Visible = false;
                    PageError(ModalPopupExtender2, lblTS, btnBack_ts, "添加失败！请联系管理员，以下为提示信息：" + ex.Message.ToString(), "UserManage3G.aspx");
                }

                if (nresultforend > 0)
                {
                    //PageRight("添加成功", "UserManage.aspx", "返回管理", "");
                    btnBack_ts.Visible = true;
                    PageRight(ModalPopupExtender2, lblTS, btnBack_ts, "<h4>添加成功</h4>", "UserManage3G.aspx");
                }
            }
            else
            {
                divMessageforloginnameservice.InnerHtml = "&nbsp;";
                string tiploginname2 = txtMemberNameforregiester.Text;
                tiploginname2 += DateTime.Now.Millisecond.ToString();
                divMessageforloginnameservice.InnerHtml = "<font style=\"color:red\">此用户名已被占用请更换后再试，比如" + tiploginname2 + "</font>";
            }
        }
    }
    #endregion

    #region 返回事件
    /// <summary>
    /// 返回事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnback_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserManage3G.aspx");
    }
    #endregion

    #region 原逻辑

    #endregion


    #region  验证手机号码
    /// <summary>
    /// 验证手机号码
    /// </summary>
    /// <param name="t"></param>
    /// <returns></returns>
    public bool strSourceNumtest(string t)
    {

        bool re = false;
        if (t.Length == 11 && (t.Substring(0, 3) == "133" || t.Substring(0, 3) == "153" || t.Substring(0, 3) == "189"))
        {
            re = true;
        }


        return re;
    }
    #endregion


    #region 全为数字
    public bool isnumber(string s)
    {
        int Flag = 0;
        char[] str = s.ToCharArray();
        for (int i = 0; i < str.Length; i++)
        {
            if (Char.IsNumber(str[i]))
            {
                Flag++;
            }
            else
            {
                Flag = -1;
                break;
            }
        }
        if (Flag > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    #endregion


    #region 初始化数据默认值绑定
    protected void ChuShiHuaData()
    {
        Initializtion(this.ddl_Unit, 0, 0, "来宾电信订餐管理系统");
    }
    #endregion

    #region 下拉框显示树型

    /// <summary>
    /// 初始化组合框级别（无限级栏目）
    /// </summary>
    /// <param name="dropList">组合框名称</param>
    /// <param name="iDepth">深度</param>
    /// <param name="iParentID">父类ID</param>
    public void Initializtion(DropDownList dropList, int iDepth, int iParentID, string strTip)
    {
        dropList.Items.Clear();
        dropList.Items.Add(new ListItem(strTip, "0"));

        ShowColumns(dropList, iDepth, iParentID);
    }

    /// <summary>
    /// 组合框显示所有级别（无限级级别）
    /// </summary>
    /// <param name="dropList">组合框名称</param>
    /// <param name="iDepth">深度</param>
    ///  <param name="iParentID">父类ID</param>int iParentID
    public void ShowColumns(DropDownList dropList, int iDepth, int iParentID)
    {



        //Cisoft.IntegrationSystem.BLL.MemberGroupBO bllgroup = new Cisoft.IntegrationSystem.BLL.MemberGroupBO();
        //Cisoft.IntegrationSystem.Model.MemberGroupMO modelgroup = new Cisoft.IntegrationSystem.Model.MemberGroupMO();
        UserGroupBO bllgroup = new UserGroupBO();
        UserGroupMO modelgroup = new UserGroupMO();
        //Power myPower = new Power();

        //定义变量
        int iCount = 0;
        int iSum = 0;
        string strGroupName = "";
        string strGroupID = "";


        DataSet myDataSet = new DataSet();

        myDataSet = bllgroup.GetListNewByRecordSort(" UG_GroupParentID =" + iParentID + "");
        iCount = myDataSet.Tables[0].Rows.Count;

        if (iCount != 0)
        {
            //iSum深度变理
            iSum = iDepth + 1;

            for (int i = 0; i < iCount; i++)
            {
                if (i == (iCount - 1))
                {
                    strGroupName = replaceWord(GetDepth("├◎" + myDataSet.Tables[0].Rows[i]["UG_GroupName"].ToString(), iSum));
                    strGroupID = myDataSet.Tables[0].Rows[i]["UG_GroupID"].ToString();

                    dropList.Items.Add(new ListItem(strGroupName, strGroupID));

                }
                else
                {
                    strGroupName = replaceWord(GetDepth("├◎" + myDataSet.Tables[0].Rows[i]["UG_GroupName"].ToString(), iSum));
                    strGroupID = myDataSet.Tables[0].Rows[i]["UG_GroupID"].ToString();

                    dropList.Items.Add(new ListItem(strGroupName, myDataSet.Tables[0].Rows[i]["UG_GroupID"].ToString()));
                }
                ShowColumns(dropList, iSum, Int32.Parse(myDataSet.Tables[0].Rows[i]["UG_GroupID"].ToString()));
            }
        }
    }

    /// <summary>
    /// 字符串插入"│"字符
    /// </summary>
    /// <param name="str">需要插入"│"的字符串</param>
    /// <param name="i">插入"│"字符串的索引位置</param>
    /// <returns></returns>
    private static string GetDepth(string strGroupName, int iCount)
    {
        for (int i = 1; i < iCount; i++)
        {
            strGroupName = strGroupName.Insert(0, "│");
        }

        return strGroupName;
    }

    /// <summary>
    /// 将指定字符串里的“&nbsp;”替换为空字符
    /// </summary>
    /// <param name="strText"></param>
    /// <returns></returns>
    private string replaceWord(string strText)
    {
        string outText = strText.Replace("&nbsp;", " ");
        return outText;
    }

    #endregion

    #region 添加至联系人 void AddToContacts(string realName,string Mobile)
    /// <summary>
    /// 添加至联系人
    /// </summary>
    /// <param name="realName">真实姓名</param>
    /// <param name="Mobile">手机号码</param>
    private void AddToContacts(string realName, string Mobile)
    {
        Cisoft.Cisoft3G.BLL.ContactsBO bll = new Cisoft.Cisoft3G.BLL.ContactsBO();
        Cisoft.Cisoft3G.Model.ContactsMO model = new Cisoft.Cisoft3G.Model.ContactsMO();

        model.CT_ContactID = bll.GetMaxId();
        model.CT_Name = realName;
        model.CT_Mobile = Mobile;

        model.CG_ContactGroupID_FK = 1;
        model.CT_Address = "";
        model.CT_Company = "";
        model.CT_CompanyAddress = "";
        model.CT_Department = "";
        model.CT_Duty = "";
        model.CT_Email = "";
        //model.CT_IsUser = 1;
        model.CT_IsUser = ddlUserGroupParent.SelectedValue.IndexOf("会员") >= 0?0:1;
        model.CT_Mobile2 = "";
        model.CT_Phone = "";
        model.CT_PostCode = "";
        model.CT_QQ = "";
        model.CT_Sex = 1;

        bll.Add(model);
    }
    #endregion
}
